package com.drinks.yiniang.ui.activity.common;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.user.UserInfoJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.utils.ScreenSizeUtils;
import com.hansen.library.utils.StringUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;

/**
 * 通用输入页面
 *
 * @author han
 * @date 2019/09/08
 */
public class PublicInputActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    private static int INPUT_TYPE_NUMBER = 0x01;

    public static final int TYPE_API_USERNAME = 0x01;//昵称
    public static final int TYPE_API_SIGNATURE = 0x02;//签名

    //nav bar
    private NavigationBarLayout nav_bar_public_input;
    //文本框
    private EditText et_public_input;
    //最大输入字数
    private TextView tv_public_input_length;

    //标题
    private String mTitles;
    //文本内容
    private String mTextContents;
    //文本提示
    private String mHints;
    private Boolean showMaxLength;
    private Boolean showBigBox;
    //是否必填, 默认必填
    private Boolean isNecessary = true;
    //文本最大字数
    private int mMaxLength;
    //文本行数,  默认1行， 0代表不限制
    private int mLineTypes;
    //文本类型,
    private int mInputType;

    //当前调用接口的类型
    private int mCurrentApiType;

    @Override
    protected void initView() {
        nav_bar_public_input = findViewById(R.id.nav_bar_public_input);

        et_public_input = findViewById(R.id.et_public_input);

        tv_public_input_length = findViewById(R.id.tv_public_input_length);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        if (getIntent() != null) {
            mTitles = getIntent().getStringExtra(Constants.KeyTitle);
            mTextContents = getIntent().getStringExtra(Constants.KeyTextContents);
            mHints = getIntent().getStringExtra(Constants.KeyHint);

            mInputType = getIntent().getIntExtra(Constants.KeyInputType, 0);
            mMaxLength = getIntent().getIntExtra(Constants.KeyMaxLength, 0);
            mLineTypes = getIntent().getIntExtra(Constants.KeyLineType, 1);
            mCurrentApiType = getIntent().getIntExtra(Constants.KeyType, -1);

            showMaxLength = getIntent().getBooleanExtra(Constants.KeyShowMax, false);
            showBigBox = getIntent().getBooleanExtra(Constants.KeyShow, false);
            isNecessary = getIntent().getBooleanExtra(Constants.keyNecessary, true);
        }

        if (mTitles != null) {
            nav_bar_public_input.setNavBarTitle(mTitles);
        }

        et_public_input.setText(mTextContents);

        if(mTextContents != null) {
            et_public_input.setSelection(mTextContents.trim().length());
        }

        if (mHints != null) {
            et_public_input.setHint(mHints);
        }

        if (mLineTypes > 0) {
            et_public_input.setMaxLines(mLineTypes);
        }

        if(mInputType == INPUT_TYPE_NUMBER) {
            et_public_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER);
        }

        if (mMaxLength > 0) {
            et_public_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mMaxLength)});

            if(showMaxLength) {
                tv_public_input_length.setVisibility(View.VISIBLE);
                tv_public_input_length.setText(String.format(getResources().getString(R.string.text_max_length), mMaxLength));
            }
        }

        if(showBigBox) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) et_public_input.getLayoutParams();

            int mHeight = ScreenSizeUtils.dp2px(mContext,260);

            params.height = mHeight;

            et_public_input.setHeight(mHeight);

            et_public_input.setLayoutParams(params);
        }
    }

    @Override
    protected void initListener() {
        nav_bar_public_input.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_public_input;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {//保存
        doModifyUserInfo();
    }

    private void doModifyUserInfo() {
        String key = mCurrentApiType == TYPE_API_USERNAME ? "username" : mCurrentApiType == TYPE_API_SIGNATURE ? "signature" : null;

        if(key == null) {
            return;
        }

        final String content = StringUtils.getEditTextString(et_public_input.getText());

        if(isNecessary) {
            if (StringUtils.isEmpty(content)) {
                String tips = mHints != null ? mHints : "请先填写信息";

                ToastUtils.showShort(tips);
                return;
            }
        }

        showLoadingDialog();

        UserApiIO.getInstance().modifyUserInfo(key, content, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                Intent intent = new Intent();
                intent.putExtra(Constants.KeyTextContents, content);
                setResult(Constants.RESULT_CODE_NORMAL, intent);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
